package cn.iocoder.yudao.module.erp.service.business.order.ordermethod;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod.vo.OrderMethodPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod.vo.OrderMethodSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.ordermethod.OrderMethodDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.order.ordermethod.OrderMethodMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.ORDER_METHOD_NOT_EXISTS;

/**
 * 结算方式 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class OrderMethodServiceImpl implements OrderMethodService {

    @Resource
    private OrderMethodMapper orderMethodMapper;

    @Override
    public Long createOrderMethod(OrderMethodSaveReqVO createReqVO) {
        // 插入
        OrderMethodDO orderMethod = BeanUtils.toBean(createReqVO, OrderMethodDO.class);
        orderMethodMapper.insert(orderMethod);
        // 返回
        return orderMethod.getId();
    }

    @Override
    public void updateOrderMethod(OrderMethodSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderMethodExists(updateReqVO.getId());
        // 更新
        OrderMethodDO updateObj = BeanUtils.toBean(updateReqVO, OrderMethodDO.class);
        orderMethodMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderMethod(Long id) {
        // 校验存在
        validateOrderMethodExists(id);
        // 删除
        orderMethodMapper.deleteById(id);
    }

    private void validateOrderMethodExists(Long id) {
        if (orderMethodMapper.selectById(id) == null) {
            throw exception(ORDER_METHOD_NOT_EXISTS);
        }
    }

    @Override
    public OrderMethodDO getOrderMethod(Long id) {
        return orderMethodMapper.selectById(id);
    }

    @Override
    public PageResult<OrderMethodDO> getOrderMethodPage(OrderMethodPageReqVO pageReqVO) {
        return orderMethodMapper.selectPage(pageReqVO);
    }

}